package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.domain.SmsHomeAdvertise;
import com.itheima.mapper.SmsHomeAdvertiseMapper;
import com.itheima.service.SmsHomeAdvertiseService;

import com.itheima.util.PageBeanVo;
import com.itheima.util.PageUtils;
import org.apache.commons.lang.StringUtils;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 首页轮播广告表 服务实现类
 * </p>
 *
 * @author zhuguo
 * @since 2022-11-10
 */
@DubboService
public class SmsHomeAdvertiseServiceImpl extends ServiceImpl<SmsHomeAdvertiseMapper, SmsHomeAdvertise> implements SmsHomeAdvertiseService {
    @Autowired
    private SmsHomeAdvertiseMapper advertiseMapper;

    @Override
    public PageBeanVo list(String name, Integer type, Integer pageNum, Integer pageSize, String endTime) {
        Page<SmsHomeAdvertise> pageInfo=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeAdvertise> lqw=new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotEmpty(name),SmsHomeAdvertise::getName,name);
        lqw.like(StrUtil.isNotEmpty(endTime),SmsHomeAdvertise::getEndTime,endTime);
        lqw.orderByDesc(SmsHomeAdvertise::getSort);
        lqw.eq(type!=null,SmsHomeAdvertise::getType,type);
        advertiseMapper.selectPage(pageInfo,lqw);
        return new PageBeanVo(pageNum,pageSize,pageInfo.getTotal(),pageInfo.getRecords());
    }

    @Override
    public void add(SmsHomeAdvertise smsHomeAdvertise) {
        advertiseMapper.insert(smsHomeAdvertise);
    }

    @Override
    public void delete(Integer[] ids) {
        for (Integer id : ids) {
            LambdaQueryWrapper<SmsHomeAdvertise> lqw=new LambdaQueryWrapper<>();
            lqw.eq(SmsHomeAdvertise::getId,id);
            advertiseMapper.delete(lqw);
        }
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        SmsHomeAdvertise smsHomeAdvertise=new SmsHomeAdvertise();
        smsHomeAdvertise.setId((long)id);
        smsHomeAdvertise.setStatus(status);
        advertiseMapper.updateById(smsHomeAdvertise);
    }

    @Override
    public void updateById(SmsHomeAdvertise smsHomeAdvertise, Integer id) {
        LambdaQueryWrapper<SmsHomeAdvertise> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SmsHomeAdvertise::getId,id);
        advertiseMapper.update(smsHomeAdvertise,lqw);
    }

    @Override
    public SmsHomeAdvertise findById(Integer id) {
        LambdaQueryWrapper<SmsHomeAdvertise> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SmsHomeAdvertise::getId,id);
        SmsHomeAdvertise smsHomeAdvertise = advertiseMapper.selectOne(lqw);
        return smsHomeAdvertise;
    }

    /*@Override
    public int add(SmsHomeAdvertise advertise) {
        advertise.setClickCount(0);
        advertise.setOrderCount(0);
        return advertiseMapper.insert(advertise);
    }

    @Override
    public int delete(List<Long> ids) {

        return advertiseMapper.deleteBatchIds(ids);
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        SmsHomeAdvertise smsHomeAdvertise = advertiseMapper.selectById(id);
        smsHomeAdvertise.setStatus(status);
        return 1;
    }

    @Override
    public int update(SmsHomeAdvertise advertise) {
        SmsHomeAdvertise smsHomeAdvertise = advertiseMapper.selectById(advertise);
       advertiseMapper.updateById(smsHomeAdvertise);
        return 1;
        //return advertiseMapper.update(advertise);
    }

    @Override
    public SmsHomeAdvertise getItem(Long id) {
        return advertiseMapper.selectById(id);
    }*/
}
